package com.sinosoft.hydra.um.dao.impl;

import com.sinosoft.hydra.um.commons.ResourceRowMapper;
import com.sinosoft.hydra.um.dao.SysResourceDao;
import com.sinosoft.hydra.um.domain.permission.SysResource;
import com.sinosoft.hydra.um.repositories.SysResourceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 资源持久化类，主要用于用户资源的显示功能。
 *
 * @author liangminga
 * @since 0.5-alpha
 * 2015年4月13日
 */
@Service
public class SysResourceDaoImpl implements SysResourceDao {
    /**
     * jdbc模板类
     */
    @Autowired
    private JdbcTemplate jdbcTemplate;
    /**
     * 资源持久化类
     */
    @Autowired
    private SysResourceRepository resourceRepository;

    public List<SysResource> findByPid(String id) {
        return jdbcTemplate.query(
                "select r.ID,r.RESOURCE_NAME,r.AVAILABLE,r.PARENT_ID,r.PERMISSION,r.RESOURCE_TYPE,r.RESOURCE_URL from UM_SYS_RESOURCE r " +
                        " where r.PARENT_ID=? and r.AVAILABLE=1 order by r.ORDER_NO asc", new Object[]{id}, new RowMapper<SysResource>() {

                    public SysResource mapRow(ResultSet rs, int rowNum) throws SQLException {
                        SysResource sysResource = new SysResource();
                        sysResource.setId(rs.getString("ID"));
                        sysResource.setResourceName(rs.getString("RESOURCE_NAME"));
                        sysResource.setPermission(rs.getString("PERMISSION"));
                        sysResource.setResourceType(rs.getString("RESOURCE_TYPE"));
                        sysResource.setAvailable(rs.getInt("AVAILABLE"));
                        sysResource.setResourceUrl(rs.getString("RESOURCE_URL"));
                        return sysResource;
                    }
                });
    }


    public List<SysResource> findAllOfFather() {
        // TODO Auto-generated method stub
        return jdbcTemplate.query(
                "select * from UM_SYS_RESOURCE r " +
                        " where r.PARENT_ID is null and r.AVAILABLE=1 order by r.ORDER_NO asc", new RowMapper<SysResource>() {

                    public SysResource mapRow(ResultSet rs, int rowNum) throws SQLException {
                        SysResource sysResource = new SysResource();
                        sysResource.setId(rs.getString("ID"));
                        sysResource.setResourceName(rs.getString("RESOURCE_NAME"));
                        sysResource.setPermission(rs.getString("PERMISSION"));
                        sysResource.setResourceType(rs.getString("RESOURCE_TYPE"));
                        sysResource.setAvailable(rs.getInt("AVAILABLE"));
                        sysResource.setResourceUrl(rs.getString("RESOURCE_URL"));
                        return sysResource;
                    }
                });
    }

    /**
     * 赋予新资源为角色
     *
     * @param li     资源主键集
     * @param roleId 角色主键
     */
    public void saveRoleResource(List<String> li, String roleId) {
        String sql = "delete FROM UM_SYS_ROLE_RESOURCE where ROLE_ID=?";
        jdbcTemplate.update(sql, roleId);
        for (String id : li) {
            if (id.length() > 0) {
                sql = "insert into UM_SYS_ROLE_RESOURCE values(?,?)";
                jdbcTemplate.update(sql, new String[]{id, roleId});
            }
        }
    }

    /**
     * 赋予角色所有资源
     *
     * @param roleId
     */
    public void saveRoleAllResource(String roleId) {
        String sql = "delete FROM UM_SYS_ROLE_RESOURCE where ROLE_ID=?";
        jdbcTemplate.update(sql, roleId);
        for (String id : resourceRepository.findAllResId()) {
            if (id.length() > 0) {
                sql = "insert into UM_SYS_ROLE_RESOURCE values(?,?)";
                jdbcTemplate.update(sql, new String[]{id, roleId});
            }
        }
    }
}
